package com.ccjt.efcfs.util;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * commons.lang3.time.DateUtils 定制新方法
 * https://www.cnblogs.com/mlq2017/p/10260216.html
 * https://blog.csdn.net/torpidcat/article/details/96432032
 * https://blog.csdn.net/u010398771/article/details/88422218
 * https://blog.csdn.net/hatsune_miku_/article/details/78620562
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {

    //默认使用系统当前时区
    public static final ZoneId LOCAL_ZONE = ZoneId.systemDefault();

    public static final String DATE_FORMAT = "yyyy-MM-dd";

    public static final String DATE_FORMAT_DEFAULT = "yyyy-MM-dd HH:mm:ss";

    public static final String TIME_NOFUII_FORMAT = "yyyyMMddHHmmss";

    public static final String REGEX = "\\:|\\-|\\s";

    public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    /**
     * Date转String("yyyy-MM-dd")
     *
     * @param date
     * @return
     */
    public static String formatDate(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
        return dateFormat.format(date);
    }

    /**
     * String("yyyy-MM-dd")转Date
     *
     * @param dateString
     * @return
     */
    public static Date parseDate(String dateString) {
        try {
            return parseDate(dateString, DATE_FORMAT);
        } catch (ParseException ex) {
            Logger.getLogger(DateUtils.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    /**
     * localDate转Date
     *
     * @param localDate
     * @return
     */
    public static Date parseDate(LocalDate localDate) {
        if (localDate == null) {
            return null;
        }
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(LOCAL_ZONE);
        Instant instant1 = zonedDateTime.toInstant();
        Date from = Date.from(instant1);
        return from;
    }

    /**
     * String 转 localDate
     *
     * @param dateString
     * @return
     */
    public static LocalDate parseLocalDate(String dateString) {
        try {
            return LocalDate.parse(dateString);
        } catch (Exception ex) {
            Logger.getLogger(DateUtils.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    /**
     * Date 转 localDate
     *
     * @param date
     * @return
     */
    public static LocalDate parseLocalDate(Date date) {
        if (date == null) {
            return null;
        }
        Instant instant = date.toInstant();
        ZonedDateTime zdt = instant.atZone(LOCAL_ZONE);
        LocalDate localDate = zdt.toLocalDate();
        return localDate;
    }

    /**
     * 获取两个日期之间的计数
     *
     * @param beforeDateString
     * @param afterDateString
     * @return
     */
    public static Integer getBetweenDays(String beforeDateString, String afterDateString) {
        return getBetweenDays(parseLocalDate(beforeDateString), parseLocalDate(afterDateString));
    }

    /**
     * 获取两个日期之间的计数
     *
     * @param beforeDate
     * @param afterDate
     * @return
     */
    public static Integer getBetweenDays(Date beforeDate, Date afterDate) {
        return getBetweenDays(parseLocalDate(beforeDate), parseLocalDate(afterDate));
    }

    /**
     * 获取两个日期之间的计数
     *
     * @param beforeLocalDate
     * @param afterLocalDate
     * @return
     */
    public static Integer getBetweenDays(LocalDate beforeLocalDate, LocalDate afterLocalDate) {
        if (beforeLocalDate != null && afterLocalDate != null) {
            return Math.abs((int) (afterLocalDate.toEpochDay() - beforeLocalDate.toEpochDay())); //取正数
        }
        return null;
    }

    /**
     * 获取两个日期之间的计数
     *
     * @param beforeDateString
     * @param afterDateString
     * @return
     */
    public static BigDecimal getBetweenBigDecimalDays(String beforeDateString, String afterDateString) {
        Integer days = getBetweenDays(parseLocalDate(beforeDateString), parseLocalDate(afterDateString));
        return days == null ? null : new BigDecimal(days);
    }

    /**
     * 获取两个日期之间的计数
     *
     * @param beforeDate
     * @param afterDate
     * @return
     */
    public static BigDecimal getBetweenBigDecimalDays(Date beforeDate, Date afterDate) {
        Integer days = getBetweenDays(parseLocalDate(beforeDate), parseLocalDate(afterDate));
        return days == null ? null : new BigDecimal(days);
    }

    public static void main(String[] args) {
        System.out.println(getBetweenDays("2020-03-01", "2020-03-08"));
        System.out.println(getBetweenDays(parseDate("2020-03-a1"), parseDate("2020-03-08")));
        System.out.println(getBetweenDays("2020-03-a1", "2020-03-08"));
        System.out.println(getBetweenBigDecimalDays("2020-03-01", "2020-03-08"));
        System.out.println(getBetweenBigDecimalDays("2020-03-a1", "2020-03-08"));
    }

}
